/**
 * File Name   : ProductInsurAction.java
 * Description : 
 */
package com.hh.admin.product.insurance.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.hh.admin.common.bean.LoginUserInfo;
import com.hh.admin.common.service.CommonService;
import com.hh.admin.product.insurance.form.ParamProductInsurBean;
import com.hh.admin.product.insurance.form.ProductInsurForm;
import com.hh.admin.product.insurance.form.ResultProductInsurBean;
import com.hh.admin.product.insurance.service.ProductInsurService;
import com.hh.admin.producttype.form.ParamProductTypeBean;
import com.hh.admin.producttype.form.ResultProductTypeBean;
import com.hh.common.bean.PortalLocalBean;
import com.hh.common.consts.AdminConst;

/**
 * 保险基金产品管理Action
 * @author 冯昌硕
 * @create 2013-1-9 下午11:37:16
 * @modify 2013-02-22 21:20 修改方法portalDetail
 * @modify 2013-02-23 11:07 修改方法portalDetail
 */
public class ProductInsurAction extends MappingDispatchAction {
    
    private static final Logger log = Logger.getLogger(ProductInsurAction.class);

    /**
     * 公共Service
     */
    private CommonService commonService;
    
    private ProductInsurService insurProService;
    
    /**
     * 获取银行理财产品列表
     * @create 2013-1-8 下午8:20:28
     */
    public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductInsurForm proForm = (ProductInsurForm) form;
        
        // 查询当前页要显示的记录
        ParamProductInsurBean paramBean = new ParamProductInsurBean();
        List<ResultProductInsurBean> productInsurLst = 
                commonService.selRecordList(proForm, "productinsur.selProductInsurInfoList", paramBean);
        request.setAttribute("productInsurLst", productInsurLst);
        
        // 查询总记录数
        int totleCnt = commonService.selRecordCount(paramBean, "productinsur.selProductInsurInfoCount");
        proForm.setTotleCnt(totleCnt);
        
        // 计算记录总页数
        int pageCnt = 1;
        pageCnt = totleCnt / proForm.getPageSize();
        if(totleCnt % proForm.getPageSize() != 0) {
            pageCnt += 1;
        }
        proForm.setTotlePage(pageCnt);
        log.debug("查询保险产品记录：totleCnt=[" + totleCnt + "] pageCnt=[" + pageCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 删除单条保险产品记录
     * @create 2013-1-10 下午10:21:33
     * @modify 2013-02-15 17:27 删除单条记录时同时删除common_product表相应记录
     */
    public ActionForward deleteOne(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductInsurForm ptForm = (ProductInsurForm) form;
        
        int id = ptForm.getId();
        log.debug("保险产品删除，要删除的产品ID=[" + id + "]");
        
        int productTypeId = ptForm.getProductTypeId();
        int delCnt = insurProService.deleteOneInsurProductInfo(id, productTypeId);
        log.debug("保险产品[" + id + "]删除，影响行数=[" + delCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 批量删除
     * @create 2013-1-10 下午10:27:14
     * @modify 2013-02-15 17:34 删除批量记录时同时删除common_product表相应记录
     */
    public ActionForward deleteMany(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductInsurForm ptForm = (ProductInsurForm) form;
        Integer[] ids = ptForm.getIds();
        
        int productTypeId = ptForm.getProductTypeId();
        int delCnt = insurProService.deleteManyProductInfo(ids, productTypeId);
        log.debug("批量删除保险产品，影响行数:[" + delCnt + "]");
        
        return mapping.findForward("list");
    }
    
    /**
     * 添加保险产品记录
     * @create 2013-1-10 下午10:30:38
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductInsurForm ptForm = (ProductInsurForm) form;
        
        LoginUserInfo loginUserInfo = 
                (LoginUserInfo) request.getSession().getAttribute(AdminConst.SESSION_ATTR_USERINFO);
        ptForm.setCreateUser(loginUserInfo.getLoginName());
        
        int newId = insurProService.insertInsurProduct(ptForm);
        log.debug("新增保险产品ID=[" + newId + "]");
        
        return mapping.findForward("success");
    }
    
    /**
     * 打开产品详情页进行内容编辑
     * @create 2013-1-10 下午10:54:03
     */
    public ActionForward detailForEdit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductInsurForm ptForm = (ProductInsurForm) form;
        int id = ptForm.getId();
        ResultProductInsurBean result = insurProService.getInsurProductDetail(id);
        
        request.setAttribute("insurProductDetail", result);
        
        return mapping.findForward("edit");
    }
    
    /**
     * 编辑后的产品内容更新
     * @create 2013-1-9 上午12:14:28
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductInsurForm ptForm = (ProductInsurForm) form;
        int updCnt = insurProService.updateInsurProductInfo(ptForm);
        log.debug("修改保险产品[" + ptForm.getId() + "]信息，修改影响行数=[" + updCnt + "]");
        
        return mapping.findForward("success");
    }
    
    /**
     * Portal页面查询使用，获取要在首页显示的保险基金内容记录
     * @create 2013-2-2 下午8:32:45
     */
    public ActionForward portalLst(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ProductInsurForm proForm = (ProductInsurForm) form;
        
        // 查询当前页要显示的记录
        ParamProductInsurBean paramBean = new ParamProductInsurBean();
        List<ResultProductInsurBean> productInsurLst = 
                commonService.selRecordList(proForm, "productinsur.selPortalProductInsurInfoList", paramBean);
        request.setAttribute("productInsurLst", productInsurLst);
        
        return mapping.findForward("portalLst");
    }
    
    /**
     * 保险基金产品前台点击链接进入到详情查看页面
     * @create 2013-2-20 下午11:03:37
     * @modify 2013-02-22 21:20 设置当前位置路径
     * @modify 2013-02-23 11:07 查询产品类型列表
     */
    public ActionForward portalDetail(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 设置当前位置路径
        PortalLocalBean firLocalBean = new PortalLocalBean();
        firLocalBean.setShowName("产品与服务");
        firLocalBean.setHref("./products.do");
        request.setAttribute("firLocalBean", firLocalBean);
        
        List<PortalLocalBean> afterLocalLst = new ArrayList<PortalLocalBean>();
        PortalLocalBean afterLocalBean = new PortalLocalBean();
        afterLocalBean.setShowName("保险基金产品");
        afterLocalBean.setHref("./products.do?typeFlag=bxjj");
        afterLocalLst.add(afterLocalBean);
        request.setAttribute("afterLocalLst", afterLocalLst);
        
        ProductInsurForm ptForm = (ProductInsurForm) form;
        
        // 查询产品类型列表
        ParamProductTypeBean paramBean = new ParamProductTypeBean();
        List<ResultProductTypeBean> producttypeLst = 
                commonService.selRecordList(ptForm, "producttype.selPortalProductTypeList", paramBean);
        request.setAttribute("producttypeLst", producttypeLst);
        
        int id = ptForm.getId();
        ResultProductInsurBean result = insurProService.getInsurProductDetail(id);
        request.setAttribute("insurProductDetail", result);
        
        return mapping.findForward("detail");
    }

    public ProductInsurService getInsurProService() {
        return insurProService;
    }

    public void setInsurProService(ProductInsurService insurProService) {
        this.insurProService = insurProService;
    }

    public void setCommonService(CommonService commonService) {
        this.commonService = commonService;
    }

    
}
